package smlab.image
{
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	/**
	 * 使用 bitmapData.draw 方法绘制指定区域
	 * @author simon
	 * 原作者:L4cd.Net
	 */
	public class BMPCutter_SM extends Sprite
	{
		public function BMPCutter_SM()
		{
			super();
		}
		
		/**
		 * 复制显示对象特定区域 
		 * @param target  显示对象
		 * @param rect    区域
		 * @return        bitmapData
		 */
		public static function draw(target:DisplayObject,rect:Rectangle):BitmapData
		{
			var matrix:Matrix = new Matrix(1,0,0,1,-rect.x,-rect.y);
			rect.x = rect.y = 0;
			var bitmapData:BitmapData = new BitmapData(rect.width,rect.height,true,0x00);
			bitmapData.draw(target,matrix,null,null,rect);
			return bitmapData;
		}
		
		
		/**
		 * 以区域复制 bitmapData; 
		 * @param target  目标BitmapData
		 * @param rect    区域
		 * @return       bitmapData
		 */
		public static function copy(target:BitmapData,rect:Rectangle):BitmapData
		{
			var bitmapData:BitmapData = new BitmapData(rect.width,rect.height,true,0x00);
			bitmapData.copyPixels(target,rect,new Point(0,0));
			return bitmapData;
		}
	}
}



/*
使用实例：

[Embed(source="2009081009150339.jpg")]
private var BmpClass:Class;

public function Intercept()
{
//bmp为源显示对象
var bmp:Bitmap = new BmpClass() as Bitmap;
addChild(bmp);
bmp.y = 100;

//方法一.BitmapData.draw
var bit1:BitmapData = BMPCutter_SM.draw(bmp,new Rectangle(150,150,100,100))
var bmp1:Bitmap = new Bitmap(bit1);
addChild(bmp1);

//方法二:BitmapData.copyPixels
var bit2:BitmapData = BMPCutter_SM.copy(bmp.bitmapData,new Rectangle(150,150,100,100))
var bmp2:Bitmap = new Bitmap(bit2);
addChild(bmp2);
bmp2.x = 150;
}

*/
